package de.urbiworx.sugar;

import java.net.MalformedURLException;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class SugarContextListener implements ServletContextListener {

    private static ThreadLocal<ServletContext> instance = new ThreadLocal<ServletContext>() {
        protected ServletContext initialValue() { return (null); }
    };
    
	public void contextDestroyed(ServletContextEvent contextEvent) {
		instance.set(null);
	}

	public void contextInitialized(ServletContextEvent contextEvent) {
		try {
			SugarConfigurationHolder.init(contextEvent.getServletContext().getResource("/WEB-INF/sugar-config.xml"));
		} catch (MalformedURLException e) {
			throw new RuntimeException(e);
		}
		instance.set(contextEvent.getServletContext());
	}
	
	public static ServletContext getServletContext()
	{
		return instance.get();
	}
}
